@echo off

title Setup is configuring Shininglore Online...

echo configuring shininglore...
echo current path: %cd%
echo p0: %0
echo p1: %1
echo p2: %2

rem variables
set mysqlpw=micronsky.net
set mypath=%cd%
set mydomain=%1
set myaddr=%2

rem determine administrative permissions
net session >nul 2>&1
if %errorlevel% neq 0 (
	call :onerror %errorlevel% "installation requires administrator privilege"
	exit /b 1
)

rem install vc2008
vcredist_x86 /q
if %errorlevel% neq 0 (
	call :onerror %errorlevel% "install vc2008"
	exit /b 1
)

rem extract usr.7z
7z x -y usr.7z >nul
if %errorlevel% neq 0 (
	call :onerror %errorlevel% "extract usr.7z"
	exit /b 1
)

rem extract content.7z
pushd web-content
..\7z x -y content.7z >nul
if %errorlevel% neq 0 (
	call :onerror %errorlevel% "extract content.7z"
	popd
	exit /b 1
)
popd

rem dynamically configure server host and IP address
pushd web-content\config
attrib -R *
..\..\xrep serverconfig.txt {server_host} %mydomain%
if %errorlevel% neq 0 (
	call :onerror %errorlevel% "xrep serverconfig.txt"
	popd
	exit /b 1
)
..\..\xrep serverinfo.txt {server_addr} %myaddr%
if %errorlevel% neq 0 (
	call :onerror %errorlevel% "xrep serverinfo.txt"
	popd
	exit /b 1
)
popd

rem mysql-odbc
pushd mysql-odbc
myodbc-installer -d -a -n "MySQL ODBC 5.2 Driver" -t "DRIVER=%cd%\myodbc5a.dll;SETUP=%cd%\myodbc5S.dll"
if %errorlevel% neq 0 (
	call :onerror %errorlevel% "register mysql odbc driver."
	popd
	exit /b 1
)
popd

rem register COM
pushd sl-server
regsvr32 /s comdlg32.ocx
if %errorlevel% neq 0 (
	call :onerror %errorlevel% "regsvr32 /s comdlg32.ocx"
	popd
	exit /b 1
)
regsvr32 /s mscomctl.ocx
if %errorlevel% neq 0 (
	call :onerror %errorlevel% "regsvr32 /s mscomctl.ocx"
	popd
	exit /b 1
)
regsvr32 /s tabctl32.ocx
if %errorlevel% neq 0 (
	call :onerror %errorlevel% "regsvr32 /s tabctl32.ocx"
	popd
	exit /b 1
)
popd

rem mysql-server
pushd mysql\bin
mysqld --install
if %errorlevel% neq 0 (
	call :onerror %errorlevel% "install mysqld service"
	popd
	exit /b 1
)
net start mysql
if %errorlevel% neq 0 (
	call :onerror %errorlevel% "start mysql service"
	popd
	exit /b 1
)
mysqladmin -h localhost -P 3306 -u root password %mysqlpw%
if %errorlevel% neq 0 (
	call :onerror %errorlevel% "change mysql root password"
	popd
	exit /b 1
)
mysql -h localhost -P 3306 -u root -p%mysqlpw% < ..\..\sldb.sql
if %errorlevel% neq 0 (
	call :onerror %errorlevel% "create sldb"
	popd
	exit /b 1
)
popd

rem apache httpd
pushd apache\bin
httpd -k install -n Apache
if %errorlevel% neq 0 (
	call :onerror %errorlevel% "install apache service"
	popd
	exit /b 1
)
net start apache
if %errorlevel% neq 0 (
	call :onerror %errorlevel% "start apache service"
	popd
	exit /b 1
)
popd

rem ok
echo install completed.
start http://%mydomain%/
pushd sl-server
start xload
popd
goto :eof


rem error report
:onerror
echo error %1: %~2
echo setup was aborted
exit /b 0

